#!/usr/bin/env python

#222.240.248.66 117000 - [26/Sep/2012:23:59:58 +0800] "GET http://xxx/xxx.htm?xxx=xxx" 200 6414 "http://xxx" "UA"
import sys
import os

class Visit:
    def __init__(self):
        self.ip = None
        self.url = None
        self.refer = None
        self.ua = None
        self.status = 0

def parse(s):
    v = Visit()
    pIp = s.find(' ')
    (pUrlStart, pUrlEnd) = extractBracket(s, pIp)
    (pStatStart, pStatEnd) = extractSpace(s, pUrlEnd)
    (pReferStart, pReferEnd) = extractBracket(s, pUrlEnd)
    (pUaStart, pUaEnd) = extractBracket(s, pReferEnd)
    
    v.ip = s[0:pIp]
    v.url = stripUrl(s[pUrlStart:pUrlEnd])
    v.status = int(s[pStatStart:pStatEnd])
    v.refer = s[pReferStart:pReferEnd]
    v.ua = s[pUaStart:pUaEnd]
    return v
    
#Url
def stripUrl(s):
    p1 = s.find(' ')
    return s[p1+1:]

def extractSpace(s, x):
    if s[x+1] == ' ': x = x + 2
    else: x = x + 1
        
    p1 = s.find(' ', x)
    return (x, p1)
    
#Bracket ""
def extractBracket(s, x):
    p1 = s.find('"', x + 1)
    p2 = s.find('"', p1 + 1)
    return (p1 + 1, p2)
    
if __name__ == "__main__":
    main()
